import Config from 'config';
import OpenApi, * as $OpenApi from '@alicloud/openapi-client';
import Dysmsapi, * as $Dysmsapi from '@alicloud/dysmsapi20170525';

// https://next.api.aliyun.com/api/Dysmsapi/2017-05-25/SendSms

export default class Client {
  /**
   * 使用AK&SK初始化账号Client 
   * @returns { Dysmsapi }
   */
  static createDysmsapiClient() {
    let config = new $OpenApi.Config(Config.get('sms'));
    config.endpoint = "dysmsapi.aliyuncs.com";
    return new Dysmsapi(config);
  }

  /**
   * 查询短信签名
   * @param { Dysmsapi } client SMS Client
   * @param { Number } pageIndex 展示第几页的签名信息
   * @param { Number } pageSize 每页展示的签名条数
   * @returns { Promise<$Dysmsapi.QuerySmsSignListResponse> }
   */
  static async querySmsSignList(client, pageIndex, pageSize) {
    let req = new $Dysmsapi.QuerySmsSignListRequest({
      pageIndex: pageIndex,
      pageSize: pageSize,
    });
    let response = await client.querySmsSignList(req);
    console.dir(response, { depth: null })
    console.log(`------------querySmsSignList success-------------`);
    return response;
  }

  /**
   * 申请短信签名
   * @param { Dysmsapi } client SMS Client
   * @param { string } signName 签名名称 required
   * @param { string } remark 短信签名场景说明，长度不超过 200 个字符。
   * @param { string } signType 签名类型。取值：0：验证码 1：通用（默认值）
   * @param { string[] } moreData 更多资料，补充上传业务证明文件或业务截图，有助于审核人员了解您的业务详情。上传到 OSS 上的文件信息。您可以通过 GetOSSInfoForUploadFile 接口获取文件信息。
   * @param { number } qualificationId 资质 ID。required
   * @param { string } applySceneContent 应用场景
   * @param { number } signSource 签名来源。required 取值：0：企事业单位的全称或简称。1：工信部备案网站的全称或简称。2：App 应用的全称或简称。3：公众号或小程序的全称或简称。4：电商平台店铺名的全称或简称。5：商标名的全称或简称。
   * @param { boolean } thirdParty 选择申请的签名为自用或他用。
   * @returns { Promise<$Dysmsapi.CreateSmsSignResponse> }
   */
  static async createSmsSign(client, signName, remark, signType, moreData, qualificationId, applySceneContent, signSource, thirdParty) {

    let req = new $Dysmsapi.CreateSmsSignRequest({
      signName, remark, signType, moreData, qualificationId, applySceneContent, signSource, thirdParty
    });

    let response = await client.createSmsSign(req);
    console.dir(response, { depth: null });

    console.log(`------------addSmsSign success-------------`);
    return response;
  }

  /**
   * 查询签名详情
   * @param { Dysmsapi } client SMS Client
   * @param { String } signName 签名名称
   * @returns { Promise<$Dysmsapi.GetSmsSignResponse> }
   */
  static async getSmsSign(client, signName) {
    let req = new $Dysmsapi.GetSmsSignRequest({
      signName
    });

    let response = await client.getSmsSign(req);
    console.dir(response, { depth: null });

    console.log(`------------getSmsSign success-------------`);
    return response;
  }

  /**
   * 发送短信
   * @param { Dysmsapi } client SMS Client
   * @param { Object } options 发送短信参数
   * @param { String } options.phoneNumbers 接收手机号
   * @param { String } options.signName 签名名称
   * @param { String } options.templateCode 短信模板CODE
   * @param { String } options.templateParam 短信模板参数 JSON字符串
   * @returns { Promise<$Dysmsapi.SendSmsResponse> }
   */
  static async sendSms(client, options) {
    let req = new $Dysmsapi.SendSmsRequest(options);

    let response = await client.sendSms(req);
    console.dir(response, { depth: null });

    console.log(`------------sendSms success-------------`);
    return response;
  }

}

// let client = Client.createDysmsapiClient();
// Client.querySmsSignList(client, 1, 10);

// Client.createSmsSign(client, '九云北海', '北京西南华邑酒店', 1, null, 450040, '微信小程序', 3, false);

// Client.getSmsSign(client, '九云北海');